package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

//todo 本周和本月的数据是有问题的，计算的是T-1，但前端传过来的时间超出了今天
// 即使需要今天的数据，传递的时间也不应该超出今天的日期
@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;

    //todo: 该接口原本是T-1的，比如7天内的数据不包含今天的数据
    // 而这个接口实现时是按照最新数据的，即当天计算在7天内
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ) {
        log.info("turnover statistics begin:{}, end:{}", begin, end);

        TurnoverReportVO turnoverReportVO=reportService.turnoverStatistics(begin,end);

        return Result.success(turnoverReportVO);

    }

    //这个数据是T-1的
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ){
        log.info("user statistics begin:{}, end:{}", begin, end);
        UserReportVO userReportVO=reportService.userStatistics(begin,end);
        return Result.success(userReportVO);
    }

    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> ordersStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ){
        log.info("orders statistics begin:{}, end:{}", begin, end);
        OrderReportVO orderReportVO=reportService.ordersStatistics(begin,end);
        return Result.success(orderReportVO);
    }

    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> getSalesTop10(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("top10 salesTop10 begin:{}, end:{}", begin, end);
        SalesTop10ReportVO salesTop10=reportService.getSalesTop10(begin,end);
        return Result.success(salesTop10);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        reportService.export(response);
    }

}
